# Loading packages and importing data
library(pacman)
## Warning: package 'pacman' was built under R version 4.3.3
p_load(dplyr, ggplot2, esquisse, plotly, DT, scales, readr, stringr)
here::i_am("07_Afia/HTML_Widget_v1.Rmd")
## here() starts at /Users/afiatyus/Library/CloudStorage/OneDrive-SharedLibraries-Emory/Ngo, Victoria - Epi Capstone - Group 8
data_filtered <- read_csv(here::here("07_Afia/data_filtered_AT.csv"))
## Rows: 603 Columns: 65
## ── Column specification ────────────────────────────────────────────────────────
## Delimiter: ","
## chr (9): Cluster, District, Gendered_HH_Type, Sex, Village, Ward, program, ...
## dbl (42): Age, Female_15_29, Female_30, KFP_Comp_Feed, KFP_Comp_Feed_2, KFP_...
## lgl (14): Participate_IGA, KFP_contbf, KFP_micronutrient, KFP_TimelyImmun, s...
##
## ℹ Use `spec()` to retrieve the full column specification for this data.
## ℹ Specify the column types or set `show_col_types = FALSE` to quiet this message.
Using plotly, I’ll create a side by side boxplot comparing the knowledge of feeding practices in each gendered household type
# Create a new variable to seperate jitter points from boxplot
data_filtered$HH_type_jitter <- as.numeric(factor(data_filtered$Gendered_HH_Type)) + 0.25 # Nudges jitter to the right
boxplot_2 <- ggplot(data_filtered %>% filter(KFP_sum != 0), aes(x= Gendered_HH_Type, y = KFP_sum, fill = program)) +
geom_boxplot(outlier.shape = NA, alpha = 0.5, width = 0.4, position = position_nudge(x = -0.25)) + # Nudges boxplots left
geom_jitter(aes(x = HH_type_jitter, text = paste("Program:", program, "<br>Feeding Practices Known: ", KFP_sum)),
width = 0.15, size = 2, alpha = 0.7)+
scale_x_discrete(labels = label_wrap(20)) +
labs(x = "",
y = "Total Feeding Practices Known",
title = "Gendered Household Type and Feeding Practices Known",
subtitle = str_wrap("This graph excludes those knowing zero (0) feeding practices to provide a clearer picture of knowledge distribution"))+
theme_minimal()
## Warning in geom_jitter(aes(x = HH_type_jitter, text = paste("Program:", :
## Ignoring unknown aesthetics: text
ggplotly(boxplot_2, tooltip = "text")
The second html widget will be a map with the median feeding practices known coloring each location. Interactive Features : Hover over each location for the summary statistics of each location (mean, median, 1st quartile, 3rd quartile, participants) Zoom into each area to drill down from country to ward, district, village, etc. Information that users can extract from the visualization – Trends in total number of feeding practices known Indication of potential regional differences in knowledge Distribution of feeding practice awareness
# Create a new variable to seperate jitter points from boxplot
data_filtered$HH_type_jitter <- as.numeric(factor(data_filtered$Gendered_HH_Type)) + 0.25 # Nudges jitter to the right
bar_chart <- ggplot(data=data_filtered, aes(fill = KFP, x = program))+
geom_bar(position="fill")+
facet_wrap(~Gendered_HH_Type)+
scale_x_discrete(labels = label_wrap(20))
ggplotly(bar_chart, tooltip = "text")
Using DT, I can also create an interactive data table that allows the program coordinators to view and sort through the cleaned and mapped dataset. These programs have never been analyzed together (to date, they’ve produced country specific reports), as a result being able to easily view the new variables (with a corresponding methodology) will aid in the applications of the research. This would exclude information like names and addresses.
# Select relevant columns for the table
data_table <- data_filtered %>%
select(program, Gendered_HH_Type, Cluster, District, Village, Ward, interventions, Age, Sex, KFP_sum, KFP, Men_15_29, Men_30, Female_15_29, Female_30)
# Create interactive DataTable.
# This code was initially generated by the 'Advanced R Code and Statistical Consultant'chatgpt
# Edited by me to round the prices to 2 decimal places
# and create more informative variable labels
# Review the Datacamp lesson on creating web-friendly datatables
# Rename columns with title case and add units in parentheses
data_table <- data_table %>%
select(
Program = program,
District = District,
Cluster = Cluster,
Village = Village,
Ward = Ward,
`Gendered Household Type` = Gendered_HH_Type,
`Age (Years)` = Age,
`Interventions Participated In` = interventions,
`Sex` = Sex,
`Total Number of Feeding Practices Known` = KFP_sum,
`Knows 1 or more feeding practices` = KFP,
`Men in the household (between 15 and 29)` = Men_15_29,
`Men in the household (over 30)` = Men_30,
`Women in the household (between 15 and 29)` = Female_15_29,
`Women in the household (over 30)` = Female_30
)
# Create interactive DataTable
datatable(data_table,
options = list(
pageLength = 10, # Show 10 rows per page by default
autoWidth = TRUE, # Auto adjust column width
searching = TRUE, # Enable search box
fillContainer = TRUE,
scrollX = TRUE,
scrollY = "400px",
dom = 'Blfrtip', # Show filter dropdowns
columnDefs = list(
list(orderable = TRUE, targets = '_all') # Make all columns sortable
)
),
rownames = FALSE, # Hide row numbers
filter = "top" # Add column filters
)